package com.ming.springboot.security;

import com.alibaba.fastjson.JSON;
import com.ming.springboot.data.Result;
import com.ming.springboot.data.ResultCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by mqsi on 2022/5/24 16:51
 */
@Component
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
		Result fail = Result.fail(ResultCode.LOGIN_FAIL.getCode(), "Login Failure");
		response.getWriter().write(JSON.toJSONString(fail));
	}
}
